// pages/login/login.js
const util = require("../../utils/util.js");
const config = require("../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceIsSupportAuth: false, //设备是否支持生物认证方式
    deviceIsHaveAuth: false, //设备内是否录入如指纹等生物信息的接口
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.userAuth();
  },

  login: function(e) {
    var userName = e.detail.value.userName;
    var password = e.detail.value.password;
    if (userName == "") {
      util.showErrorToast('用户名不能为空');
    }
    if (password == "") {
      util.showErrorToast('密码不能为空');
    }
    var that = this;
    let url = config.BaseUrl + "log/login";
    let data = {
      userName: userName,
      password: password
    }
    util.request(url, data, "POST").then(function(res) {
      if (res.code == 200) {
        wx.setStorageSync("userInfo", res.data)
        util.showSuccessToast("登陆成功");
        wx.switchTab({
          url: '/pages/index/index',
        })
      } else {
        util.showErrorToast('登陆失败，请检查用户名密码是否正确');
      }
    })
  },
  /**
 * 生物认证
 */
  userAuth: function () {
    var that = this
    console.log(wx.canIUse("checkIsSupportSoterAuthentication"));
    wx.checkIsSupportSoterAuthentication({
      success(res) {
        console.log(res.supportMode)
        if (res.supportMode.indexOf('fingerPrint') > -1) {
          wx.checkIsSoterEnrolledInDevice({ //获取设备内是否录入如指纹等生物信息的接口
            checkAuthMode: 'fingerPrint',
            success(res) {
              console.log(res.isEnrolled),
                console.log(res.errMsg),
                that.setData({
                  deviceIsSupportAuth: true, //设备是否支持生物认证方式
                  deviceIsHaveAuth: true, //设备内是否录入如指纹等生物信息的接口
                })
              wx.startSoterAuthentication({
                requestAuthModes: ['fingerPrint'],
                challenge: '123456',
                authContent: '请用指纹解锁',
                success(res) {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              })
            },
            fail(res) {
              console.log("当前设备内没有录入如指纹等生物信息的接口" + res)
            }
          });
        }
      },
      fail(res) {
        console.log("当前设备不支持生物认证" + res)
      }
    });
  },

})